package cn.gcxy.handler;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gcxy.entity.Page;
import cn.gcxy.entity.Type;
import cn.gcxy.service.CommodityService;
import cn.gcxy.service.TypeService;

@Controller
@RequestMapping("shop")
public class ShopHandler {
	
	@Autowired
	@Qualifier(value="typeService")
	TypeService typeService;
	
	@Autowired
	@Qualifier(value="commodityService")
	CommodityService commodityService;

	@ResponseBody
	@RequestMapping(value="jumpPage")
	public void lumpPage(@RequestParam(value="bigType") String bigType, @RequestParam(value="keyWord") String keyWord,
			@RequestParam(value="jump") int jump, @RequestParam(value="pageSize") int pageSize, HttpServletRequest reuqest, Model model,HttpSession session) {
		List<Type> types = typeService.queryTypeByPid(bigType);
		Page page = (Page) session.getAttribute("page");
		page.setPageSize(pageSize);
		if(jump == -1) {
			if((page.getCurrentPage()+1) != page.getTotalPage()) {
				page.setCurrentPage(page.getCurrentPage()+1);
			}else {
				return ;
			}
		}else if(jump == -2) {
			if(page.getCurrentPage() != 0) {
				page.setCurrentPage(page.getCurrentPage()-1);
			}else {
				return ;
			}
		}else if(jump == -3) {
			page.setCurrentPage(0);
		}else if(jump == -4) {
			page.setCurrentPage(page.getTotalPage()-1);
		}else {
			page.setCurrentPage(jump);
		}
		session.setAttribute("page", commodityService.queryCommodityByTypeAndKeyWordPage(bigType,keyWord,page));
	}
	

}
